package sunlight.validation;

public class Validate {
	
	public boolean is_valid_text_length(int min, String text){
		return text.length() >= min ? true : false;
	}
	
	public boolean is_float_number(String value){
		try{
			Float fv = Float.parseFloat(value);
			
			if(fv != null){
				return true;
			}
		}catch(NumberFormatException e){
			return false;
		}
		
		return false;
	}
	
	public boolean is_int_number(String value){
		try{
			Integer iv = Integer.parseInt(value);
			
			return iv != null ? true : false;
		}
		catch(NumberFormatException e){
			return false;
		}
	}
	
	public boolean is_positive_number(String value){
		try{
			Float fv = Float.parseFloat(value);
			
			if((fv!= null) && (fv >= 0)){
				return true;
			}
			
			return false;
		}catch(NumberFormatException e){
			return false;
		}
	}
	
	public boolean is_negative_number(String value){
		try{
			Integer iv = Integer.parseInt(value);
			
			if((iv != null) && (iv < 0)){
				return true;
			}
		}
		catch(NumberFormatException e){
			return false;
		}
		
		return false;
	}
	
	public boolean is_smaller_then_number(int maxValue, String text){
		try{
			Float fv = Float.parseFloat(text);
			
			if((fv != null) && (fv < maxValue)){
				return true;
			}
		}
		catch(NumberFormatException e){
			System.out.println("exceptie " + e.toString());
			return false;
		}
		
		return false;
	}
	
	public boolean is_valid_ip(String ip){
		String ValidIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
		
		if(ip.matches(ValidIpAddressRegex)){
			return true;
		}
		
		return false;
	}
	
	public boolean is_valid_hostname(String host){

		String ValidHostnameRegex = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\\-]*[A-Za-z0-9])$";

		
		if(host.equals("localhost")){
			  return true;
		}
		
		if(host.matches(ValidHostnameRegex)){
			return true;
		}
		
		return false;
	}
	
	public boolean is_valid_url(String url){
		  url = url.toLowerCase();
		  url = url.trim();
		  
		  if((url.startsWith("http://")) || (url.startsWith("https://"))){
			  if(url.matches("^((https?|ftp)://|(www|ftp).)[a-z0-9-]+(.[a-z0-9-]+)+([/?].*)?$")){
				  return true;
			  }
		  }
		  
		  return false;
	  }
	
	public boolean is_valid_cnp(String cnp){
		return cnpValidator.is_valid(cnp);
	}
	
	//TODO finish this function... 
	public boolean is_valid_email(String email){
		
		String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
		
		if((email.length() > 0) && email.matches(EMAIL_PATTERN)){
			return true;
		}
		
		return false;
	}
}
